﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Extensions
{
    public static class StringExtensions
    {
        public static string ToHtmlEncodedString(this string str) 
        { 
            return string.Join("", str.ToCharArray().Select(c => (int)c > 127 ? "&#" + (int)c + ";" : c.ToString()).ToArray()); 
        }

        public static string ToMD5HashedString(this string data)
        {
            MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
            byte[] hashedBytes = csp.ComputeHash(bytes, 0, bytes.Length);
            return BitConverter.ToString(hashedBytes).Replace("-", String.Empty).ToLower(System.Globalization.CultureInfo.InvariantCulture);
        }
    }
}